<?php
/**
 * 字段挂件 - 管理后台
 *
 * @author birdy <freebirdy@foxmail.com>
 * @link http://www.aiyuangong.com
 */

namespace app\admin\widget;

use think\Config;
class Fields
{
    /**
     * 字段编辑挂件
     * @param array|string $json     字段初始化信息
     * @param string       $save_url 处理地址
     * @return string
     */
    public function editor($json = [], $save_url='')
    {
        if (!$save_url) {
            $save_url = request()->baseUrl();
        }
        if (!is_array($json)) {
            $json = json_decode($json, true);
        }
        
        return view('common/fields/editor', ['save_url'=>$save_url, 'fields'=>$json ? : []]);
    }
    
    /**
     * 字段编辑挂件
     * @param array|string $fields 字段初始化信息
     * @param array        $values 字段值
     * @param string       $tpl    模板
     * @param string       $prefix 表单字段
     * @param string       $type   字段类型
     * @return string
     */
    public function form($fields = [], $values = [], $tpl = '', $prefix = '', $type = '')
    {
        $tpl    = $tpl ? : Config::get('fields.tpl')['form'];
        $prefix = $prefix ? : Config::get('fields.field_name');
        $type   = $type ?   : Config::get('fields.field_type');
        if (!is_array($fields)) {
            $fields = json_decode($fields, true);
        }
        
        foreach ($fields as $k=>$v) {
            $name = $v['name'];
            switch ($v['type']) {
                case 'checkbox':
                case 'select':
                case 'radio':
                    $fields[$k]['option'] = \Fields::parse_option($v['option']);
                    break;
                case "type":
                    $type = \Fields::getById($v['option']);
                    $option = [];
                    foreach ($type as $type) {
                        $option[$type['id']] = $type['name'];
                    }
                    $fields[$k]['option'] = $option;
                    break;
                case "config":
                    $option = \Fields::parse_option(Config::get($v['option']), "\r\n");
                    $fields[$k]['option'] = $option;
                    break;
                case "table":
                    $option = explode("|", $v['option']);
                    $fields[$k]['option'] = \think\Db::name($option[0])->limit(100)->column($option[1]);
                    break;
            }
            
            //赋值初始值
            $fields[$k]['value'] = '';
            if (isset($values[$name])) { //已经设置过
                $fields[$k]['value'] = $values[$name];
            } else if ($v['default'] !== '') { //默认值
                $fields[$k]['value'] = $v['default'];
            }
            
            if ($type == 'array') {
                $fields[$k]['name_form'] = trim($prefix ? $prefix."[".$v['name']."]" : $v['name']);
            } else {
                $fields[$k]['name_form'] = trim($prefix ? $prefix.$v['name'] : $v['name']);
            }
        }
        
        $view = new \think\View();
        return $view->fetch($tpl, ['fields'=>$fields]);
    }
}